package com.androinject.core.aware;

import com.androinject.core.definitions.ComponentDefinition;
import com.androinject.core.definitions.ManagedComponentDefinition;
import com.androinject.core.factory.AbstractComponentProcessor;

abstract public class AbstractAwareProcessor<T> extends AbstractComponentProcessor {
	
	protected Class<T> awareClass;
	
	
	protected AbstractAwareProcessor(Class<T> awareClass) {
		this.awareClass = awareClass;
	}
	
	
	@Override
	public void beforeComponentInjection(Object component, ComponentDefinition definition) {
		super.beforeComponentInjection(component, definition);
		if (this.awareClass.isAssignableFrom(component.getClass())) {
			T castedComponent = this.awareClass.cast(component);
			this.handleComponent(castedComponent, definition);
		}
	}
	
	
	@Override
	public void beforeManagedComponentInjection(Object component, ManagedComponentDefinition definition) {
		super.beforeManagedComponentInjection(component, definition);
		if (this.awareClass.isAssignableFrom(component.getClass())) {
			T castedComponent = this.awareClass.cast(component);
			this.handleManagedComponent(castedComponent, definition);
		}
	}
	
	
	protected Class<T> getAwareClass() {
		return this.awareClass;
	}
	
	
	abstract protected void handleComponent(T component, ComponentDefinition definition);
	
	
	abstract protected void handleManagedComponent(T component, ManagedComponentDefinition definition);

}
